package com.wskh.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wskh.commonUtil.R;
import com.wskh.edu.entity.Teacher;
import com.wskh.edu.entity.vo.TeacherQuery;
import com.wskh.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author wskh
 * @since 2021-10-13
 */
@Api(value = "讲师管理")
@RestController
@RequestMapping("/eduService/teacher")
@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE}) // 允许请求跨域
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "获取所有讲师")
    @GetMapping("findAll")
    public R list(){
        return R.ok().data("items",teacherService.list(null));
    }

    @ApiOperation(value = "逻辑删除讲师")
    @PostMapping("removeById/{id}")
    public R removeById(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){
        boolean b = teacherService.removeById(id);
        if(b){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "分页查询讲师列表")
    @GetMapping("pageTeacher/{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){
        Page<Teacher> pageParam = new Page<>(page, limit);
        teacherService.page(pageParam, null);
        List<Teacher> records = pageParam.getRecords();
        // 总记录数
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "条件分页查询讲师列表")
    @PostMapping("pageTeacherByOption/{current}/{limit}")
    public R pageTeacherByOption(@PathVariable long current, @PathVariable long limit,
                                    // RequestBody：自动将前端传过来的json转换为对应的对象
                                    // required = false，对象中的某些属性可以为空
                                    @RequestBody(required = false)TeacherQuery teacherQuery){
        // 创建page对象
        Page<Teacher> teacherPage = new Page<>(current,limit);
        // 构建条件
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        // 排序
        queryWrapper.orderByDesc("gmt_create");
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level) ) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }
        // 调用方法实现 分页条件查询
        IPage<Teacher> page = teacherService.page(teacherPage, queryWrapper);
        List<Teacher> records = page.getRecords();
        return R.ok().data("total",page.getTotal()).data("rows",records);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("addTeacher")
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody(required = true) Teacher teacher){
        teacherService.save(teacher);
        return R.ok();
    }

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("getTeacherById/{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("items", teacher);
    }

    @ApiOperation(value = "根据ID修改讲师")
    @PostMapping("updateTeacherById/{id}")
    public R updateById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id,
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody Teacher teacher){
        teacher.setId(id);
        System.out.println(teacher);
        teacherService.updateById(teacher);
        return R.ok();
    }

}

